/**
 * Created by wangjian on 17/4/20.
 */
myApp.controller('EditionIntegrationTemplateCtrl', function ($scope, $uibModal, $uibModalInstance, info, $http, ngDialog) {
    $scope.info = {};
    $scope.info.status = false;
    //选中工程名
    $scope.info.projectName = info.projectName;
    $scope.info.type = info.type;
    //选中工程信息(uplan)
    $scope.info.projectInfo = {};
    //申请记录
    $scope.info.recordList = [];
    //选中的记录
    $scope.info.selectRecord = null;

    doBeetleGet($http, $uibModal, beetle_plan_url + '/project/getProjectInfo?token_id=' + getTokenId() + '&project_name=' + $scope.info.projectName, function (data) {
        if (!data.success) {
            openDialogAlert(ngDialog, $scope, data.msg);
            return
        }
        $scope.info.projectInfo = data.data;
        if ($scope.info.projectInfo.isOnBeetle == '1' && $scope.info.projectInfo.package_type != 'ad') {
            openDialogAlert(ngDialog, $scope, "工程" + $scope.info.projectName + "没有用beetle管理!");
            return
        }
        if ($scope.info.projectInfo.package_type == 'maven_jar') {
            openDialogAlert(ngDialog, $scope, "jar包工程不需要提交版本!");
            return
        }
        if ($scope.info.projectInfo.plan_id == null) {
            openDialogAlert(ngDialog, $scope, "工程"+$scope.info.projectName+"已经不在任何项目内!");
            return
        }

        var type = 'TEST';
        if ($scope.info.type == 'INTEGRATION' || $scope.info.type == 'CAN_INTEGRATION' || $scope.info.type == 'SEARCH') {
            type = 'INTEGRATION';
        }
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project/getPHORecordList?token_id=' + getTokenId() + '&project_name=' + $scope.info.projectName + '&type=' + type, function (data) {
            $scope.info.recordList = data.data;
            angular.forEach($scope.info.recordList, function (r) {
                r.click = false;
            });
            $scope.info.status = true;
        })
    });

    $scope.clickRecord = function (reocrd) {
        angular.forEach($scope.info.recordList, function (r) {
            r.click = false;
        });
        reocrd.click = true;
        $scope.info.selectRecord = reocrd;
        $scope.info.selectRecord.sqlList = [];
        $scope.info.selectRecord.configList = [];
        if($scope.info.selectRecord.sql_desc != null){
            var sqlList = $scope.info.selectRecord.sql_desc.split('##');
            angular.forEach(sqlList, function (sql) {
                if (sql == '') {
                    return
                }
                var obj = {};
                obj.file_name = sql.split('@@')[0];
                obj.file_path = sql.split('@@')[1];
                $scope.info.selectRecord.sqlList.push(obj);
            });
        }
        if($scope.info.selectRecord.config_desc != null){
            var configList = $scope.info.selectRecord.config_desc.split('##');
            angular.forEach(configList, function (config) {
                if (config == '') {
                    return
                }
                var obj = {};
                obj.fileName = config.split('@@')[0];
                obj.key = config.split('@@')[1];
                obj.value = config.split('@@')[2];
                obj.desc = config.split('@@')[3];
                $scope.info.selectRecord.configList.push(obj);
            });
        }
    };

    $scope.downloadSqlFile = function (sql) {
        window.location.href = java_url + "/beetle/projectREST/downloadProjectSql?filePath=" + sql.file_path
            + "&fileName=" + sql.file_name + "&tokenId=" + getTokenId();
    };

    $scope.operateFun = function (record,type) {
        var msg = '';
        var data = {
            "token_id": getTokenId(),
            "id": record.id
        };
        if ($scope.info.type == 'TEST') {
            data.is_function_test = '0';
            msg = '确定要测试版本' + record.edition_num + '吗？';
        } else if ($scope.info.type == 'INTEGRATION') {
            data.is_integration_test = '0';
            msg = '确定要测试版本' + record.edition_num + '吗？';
        } else if ($scope.info.type == 'CAN_INTEGRATION') {
            if(type){
                data.is_can_integration = '0';
                msg = '确定版本' + record.edition_num + '进入集成环境吗？';
            }else {
                data.is_can_integration = '1';
                msg = '确定版本' + record.edition_num + '不进入集成环境吗？';
            }
        }

        openDialog(ngDialog, $scope, msg, true, function (val) {
            if (!val) {
                return;
            }
            doBeetlePost($http, $uibModal, beetle_plan_url + '/project/updatePHORecordStatus', data, function (data) {
                if (!data.success) {
                    openDialogAlert(ngDialog, $scope, data.msg);
                    return
                }
                $uibModalInstance.dismiss(data.data);
            });
        });
    };

    $scope.cancel = function () {
        $uibModalInstance.close('cancel');
    };
});